package com.sgb.member.view_model;

import android.view.View;

import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.sgb.member.R;
import com.sgb.member.di.repository.MemberRepertory;
import com.sgb.member.entity.MemberProfileBean;
import com.sgb.member.entity.PeronMemberJoinEntity;
import com.sgb.member.entity.PersonJoinDetailBean;
import com.sgb.member.entity.QualityMemberBean;
import com.sgb.member.entity.UserMsgEntity;
import com.sgb.member.ui.activity.PersonApplyJoinActivity;
import com.sgb.member.weight.SimpleResult;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.customview.ItemInputTextView;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;
import com.swgk.core.view.selection.CommonPopEntity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;

public class PersonApplyJoinModel extends BaseViewModel {

    private final PersonApplyJoinActivity activity;
    private final MemberRepertory mRepertory;

    public PersonApplyJoinModel(PersonApplyJoinActivity activity, MemberRepertory repertory) {
        this.activity = activity;
        this.mRepertory = repertory;
    }

    /**
     * 加盟企业
     */
    public void memberJoin(PeronMemberJoinEntity entity) {
        DialogHelper.showProgressDialog(activity, "数据获取中...");
        mRepertory.getApi().personMemberJoin(entity).enqueue(new ICallBack<BaseEntity<BaseEntity<String>>>() {
            @Override
            public void onSuccess(BaseEntity<BaseEntity<String>> data) {
                if (data.getData().getCode().equals("0")) {
                    activity.memberJoinSuccess();
                } else {
                    MToast.showToast(activity, data.getData().getMsg());
                }
            }
        });
    }

    public void getMemberDetail(String no) {
        DialogHelper.showProgressDialog(activity, "数据获取中...");
        mRepertory.getApi().getMemberDetail(no).enqueue(new ICallBack<BaseEntity<PersonJoinDetailBean>>() {
            @Override
            public void onSuccess(BaseEntity<PersonJoinDetailBean> data) {
                if (data != null && data.getData() != null) {
                    activity.setApplyRecordDetail(data.getData());
                } else {
                    activity.showEmptyView();
                }
            }
        });
    }

    public void getCompanyProfile(String id) {
        DialogHelper.showProgressDialog(activity, "数据获取中...");
        Map<String, Object> map = new HashMap<>();
        map.put("companyNo", id);
        mRepertory.getApi().getCompanyWebProfile(map).enqueue(new ICallBack<BaseEntity<MemberProfileBean>>() {
            @Override
            public void onSuccess(BaseEntity<MemberProfileBean> data) {
                if (data != null && data.getData() != null) {
                    activity.setMemberProfile(data.getData());
                } else {
                    activity.showEmptyView();
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<MemberProfileBean>> call, Throwable t) {
                super.onFailure(call, t);
                activity.showEmptyView();
            }
        });
    }

    // 会员等级
    private void getMemberLevelList(ItemInputTextView textView, SimpleResult<CommonPopEntity> simpleResult) {
        DialogHelper.showProgressDialog(activity, "数据获取中...");
        mRepertory.getApi().getMemberLevelList().enqueue(new ICallBack<BaseEntity<List<CommonPopEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<CommonPopEntity>> data) {
                List<CommonPopEntity> tempList = data.getData();
                if (tempList != null && tempList.size() > 0) {
                    if (tempList.get(0).getTypeName().equals("全部")) {
                        tempList.remove(0);
                    }
                    mPop4 = new ArrayList<>();
                    mPop4.addAll(tempList);
                    showMemberLeverPop(textView, simpleResult);
                }
            }
        });
    }

    private OptionsPickerView<CommonPopEntity> mOption4;
    private List<CommonPopEntity> mPop4;

    public void showMemberLeverPop(ItemInputTextView textView, SimpleResult<CommonPopEntity> simpleResult) {
        if (mPop4 == null) {
            getMemberLevelList(textView, simpleResult);
            return;
        }
        mOption4 = new OptionsPickerBuilder(activity, new OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int options2, int options3, View v) {
                String name = mPop4.get(options1).getPickerViewText();
                textView.setContent(name);
                if (simpleResult != null) {
                    simpleResult.onResult(mPop4.get(options1));
                }
            }
        }).setCancelColor(activity.getResources().getColor(R.color.color_title_small))
                .setLineSpacingMultiplier(2.2f)
                .setCancelColor(0xFF666666)
                .setTitleColor(0xFF969696)
                .setSubCalSize(15)
                .setTitleSize(15)
                .setContentTextSize(15)
                .build();
        mOption4.setTitleText("会员等级");
        mOption4.setPicker(mPop4);
        mOption4.setSelectOptions(0);
        mOption4.show();
    }

    private OptionsPickerView<CommonPopEntity> mOption5;
    private List<CommonPopEntity> mPop5;

    public void showSupplyDemandPop(ItemInputTextView textView, SimpleResult<CommonPopEntity> simpleResult) {
        if (mOption5 == null) {
            // 供需关系  1供方  2需方
            mPop5 = new ArrayList<>();
            mPop5.add(new CommonPopEntity("1", "供方会员"));
            mPop5.add(new CommonPopEntity("2", "需方会员"));
            mOption5 = new OptionsPickerBuilder(activity, new OnOptionsSelectListener() {
                @Override
                public void onOptionsSelect(int options1, int options2, int options3, View v) {
                    String no = mPop5.get(options1).getNo();
                    String name = mPop5.get(options1).getTypeName();
                    textView.getContent_edt().setText(name);
                    if (simpleResult != null) {
                        simpleResult.onResult(mPop5.get(options1));
                    }
                }
            }).setCancelColor(activity.getResources().getColor(R.color.color_title_small))
                    .setLineSpacingMultiplier(2.2f)
                    .setCancelColor(0xFF666666)
                    .setTitleColor(0xFF969696)
                    .setSubCalSize(15)
                    .setTitleSize(15)
                    .setContentTextSize(15)
                    .build();
            mOption5.setTitleText("供需关系");
        }
        mOption5.setPicker(mPop5);
        mOption5.setSelectOptions(0);
        mOption5.show();
    }
}
